package org.android.brasil.projetos.oktoberfest2011.view;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.utils.Constantes;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;
import org.android.brasil.projetos.oktoberfest2011.utils.DataUtil;
import org.android.brasil.projetos.oktoberfest2011.view.controller.HomeController;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class BuscarView extends Activity {
	private AnalyticsUtils mGa;
	private EditText etTermo;
	private Spinner spCategoria;
	private Spinner spData;
	private Spinner spLocal;
	private Button botao;

	private String[] categorias = { "Selecione um Categoria", Constantes.SHOW,
			Constantes.RETRETA, Constantes.DESFILE };

	private String[] datas = new String[Constantes.DIA_FIM_OKTOBERFEST
			- Constantes.DIA_INICIO_OKTOBERFEST + 1];

	private String[] locais = { "Selecione um Local", Constantes.SETOR1,
			Constantes.SETOR2, Constantes.SETOR3, Constantes.SETOR4,
			Constantes.PARQUE_VILA_GERMANICA, Constantes.PRACA_DR_BLUMENAU,
			Constantes.PRACA_PREFEITURA, Constantes.XV_DE_NOVEMBRO,
			Constantes.PALANQUE_BB };

	private OnClickListener casaListener = new OnClickListener() {

		public void onClick(View v) {
			Intent i = new Intent(getApplicationContext(), HomeController.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			mGa.buttonTrackEvent("Casa");
		}
	};

	private OnClickListener buscarListener = new OnClickListener() {

		public void onClick(View v) {
			mGa.buttonTrackEvent("Busca");
			if (validarCampos()) {
				String text = etTermo.getText().toString().trim();
				String cat = spCategoria.getSelectedItem().toString();
				String local = spLocal.getSelectedItem().toString();
				int data = spData.getSelectedItemPosition()
						+ Constantes.DIA_INICIO_OKTOBERFEST - 1;

				if (spCategoria.getSelectedItemPosition() == 0) {
					cat = "";
				}
				if (spData.getSelectedItemPosition() == 0) {
					data = 0;
				}
				if (spLocal.getSelectedItemPosition() == 0) {
					local = "";
				}
				Intent i = new Intent(getApplicationContext(),
						BuscarResultados.class);
				i.putExtra("TEXTO", text);
				i.putExtra("CATEGORIA", cat);
				i.putExtra("LOCAL", local);
				i.putExtra("DATA", data);
				startActivity(i);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mGa = AnalyticsUtils.getInstance(this);

		setContentView(R.layout.buscar_atividade);
		bindScreenFields();
		populateFields();

		this.mGa.trackScreen(getString(R.string.buscar) + " - view");
	}

	private void bindScreenFields() {
		etTermo = (EditText) findViewById(R.idBuscar.texto);
		spCategoria = (Spinner) findViewById(R.idBuscar.sp_categoria);
		spData = (Spinner) findViewById(R.idBuscar.sp_data);
		spLocal = (Spinner) findViewById(R.idBuscar.sp_local);
		botao = (Button) findViewById(R.idBuscar.bt_ir);
		botao.setOnClickListener(buscarListener);
		ImageView casa = (ImageView) findViewById(R.id.casa);
		casa.setOnClickListener(casaListener);

	}

	private void populateFields() {
		ArrayAdapter<String> categoriaAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, categorias);
		categoriaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCategoria.setAdapter(categoriaAdapter);

		datas[0] = "Selecione uma data";

		for (int i = 1; i < datas.length; i++) {
			int k = Constantes.DIA_INICIO_OKTOBERFEST + i - 1;
			datas[i] = DataUtil.retornarDiaSemana(
					Constantes.ANO_DA_OKTOBERFEST,
					Constantes.CALENDAR_MES_DA_OKTOBERFEST, k)
					+ ", "
					+ k
					+ " de "
					+ DataUtil
							.retornarMesDoAno(Constantes.CALENDAR_MES_DA_OKTOBERFEST);
		}
		ArrayAdapter<String> adapterData = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, datas);
		adapterData
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spData.setAdapter(adapterData);

		ArrayAdapter<String> localAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, locais);
		localAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spLocal.setAdapter(localAdapter);

		RelativeLayout rl = (RelativeLayout) findViewById(R.idBuscar.relativeLayout01);
		ControleDeBackground.SetBarraSuperiorBackGround(
				getApplicationContext(), rl);
	}

	private boolean validarCampos() {
		boolean valido = true;
		if (etTermo.getText().toString().trim().equals("")) {
			if (spCategoria.getSelectedItemPosition() == 0) {

				if (spData.getSelectedItemPosition() == 0) {

					if (spLocal.getSelectedItemPosition() == 0) {
						valido = false;
					}
				}
			}
		}
		if (!valido) {
			Toast.makeText(getApplicationContext(), "Selecione alguma opção!",
					Toast.LENGTH_LONG).show();
		}
		return valido;
	}
}
